package com.management.demo.controller.mall;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.demo.common.dto.PageDTO;
import com.management.demo.common.enums.AttrEnum;
import com.management.demo.common.vo.PageVO;
import com.management.demo.domain.mall.PmsAttr;
import com.management.demo.domain.mall.PmsAttrGroup;
import com.management.demo.domain.mall.PmsAttrGroupRelation;
import com.management.demo.domain.mall.PmsCategory;
import com.management.demo.dto.mall.PmsAttrDTO;
import com.management.demo.dto.mall.PmsAttrQueryDTO;
import com.management.demo.service.PmsAttrGroupRelationService;
import com.management.demo.service.PmsAttrGroupService;
import com.management.demo.service.PmsAttrService;
import com.management.demo.service.PmsCategoryService;
import com.management.demo.vo.mall.PmsAttrVO;
import com.management.demo.vo.mall.PmsBrandVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "属性管理")
@RestController
@RequestMapping("/pms/attr")
public class PmsAttrController {

    @Autowired
    private PmsAttrService pmsAttrService;

    @Autowired
    private PmsCategoryService pmsCategoryService;

    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;

    @Autowired
    private PmsAttrGroupRelationService relationService;

    @Operation(summary = "分页查询")
    @PostMapping("page")
    public SaResult page(@RequestBody PmsAttrQueryDTO pmsAttrQueryDTO) {
        String name = pmsAttrQueryDTO.getName();
        String attrType = pmsAttrQueryDTO.getAttrType();
        Integer currentPage = pmsAttrQueryDTO.getCurrentPage();
        Integer pageSize = pmsAttrQueryDTO.getPageSize();

        LambdaQueryWrapper<PmsAttr> queryWrapper = new LambdaQueryWrapper<PmsAttr>()
                .eq(StringUtils.isNotBlank(attrType), PmsAttr::getAttrType, attrType)
                .like(StringUtils.isNotBlank(name), PmsAttr::getName, name);
        Page<PmsAttr> pmsAttrPage = pmsAttrService.page(new Page<>(currentPage, pageSize), queryWrapper);

        PageVO<PmsAttrVO> pmsAttrPageVO = PageDTO.getPageVO(pmsAttrPage, item -> {
            PmsAttrVO pmsAttrVO = BeanUtil.copyProperties(item, PmsAttrVO.class);

            // 查询所属分类
            PmsCategory pmsCategory = pmsCategoryService.getById(pmsAttrVO.getCategoryId());
            pmsAttrVO.setCategoryName(pmsCategory.getName());

            // 如果是规格参数 通过 分组属性关联表 查询出 所属分组
            if (AttrEnum.ATTR_SPEC_TYPE.getId().equals(pmsAttrVO.getAttrType())) {
                Long id = pmsAttrVO.getId();
                PmsAttrGroupRelation pmsAttrGroupRelation = relationService.getOne(new LambdaQueryWrapper<PmsAttrGroupRelation>().eq(PmsAttrGroupRelation::getAttrId, id));
                if (pmsAttrGroupRelation != null && pmsAttrGroupRelation.getGroupId() != null) {
                    PmsAttrGroup pmsAttrGroup = pmsAttrGroupService.getById(pmsAttrGroupRelation.getGroupId());
                    pmsAttrVO.setGroupName(pmsAttrGroup.getName());
                }
            }
            return pmsAttrVO;
        });
        return SaResult.ok().setData(pmsAttrPageVO);
    }

    @Operation(summary = "查询所有")
    @GetMapping("list")
    public SaResult list() {
        List<PmsAttr> pmsBrands = pmsAttrService.list();
        return SaResult.ok().setData(BeanUtil.copyToList(pmsBrands, PmsBrandVO.class));
    }

    @Transactional
    @Operation(summary = "新增/修改")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody PmsAttrDTO pmsAttrDTO) {
        PmsAttr pmsAttr = BeanUtil.copyProperties(pmsAttrDTO, PmsAttr.class);
        boolean result = pmsAttrService.saveOrUpdate(pmsAttr);

        Long attrId = pmsAttr.getId();
        Long groupId = pmsAttrDTO.getGroupId();
        PmsAttrGroupRelation pmsAttrGroupRelation = new PmsAttrGroupRelation();
        pmsAttrGroupRelation.setAttrId(attrId);
        pmsAttrGroupRelation.setGroupId(groupId);
        boolean update = relationService.update(pmsAttrGroupRelation, new LambdaQueryWrapper<PmsAttrGroupRelation>().eq(PmsAttrGroupRelation::getAttrId, attrId));
        if (!update) {
            relationService.save(pmsAttrGroupRelation);
        }
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "根据id获取信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "id") @PathVariable Long id) {
        PmsAttr pmsAttr = pmsAttrService.getById(id);
        PmsAttrVO pmsAttrVO = BeanUtil.copyProperties(pmsAttr, PmsAttrVO.class);
        // 查询所属分类的级联属性
        Long categoryId = pmsAttrVO.getCategoryId();
        pmsAttrVO.setCategoryArray(pmsCategoryService.findCategoryPath(categoryId));
        // 所属分组id
        if (AttrEnum.ATTR_SPEC_TYPE.getId().equals(pmsAttrVO.getAttrType())) {
            PmsAttrGroupRelation pmsAttrGroupRelation = relationService.getOne(new LambdaQueryWrapper<PmsAttrGroupRelation>().eq(PmsAttrGroupRelation::getAttrId, id));
            if (pmsAttrGroupRelation != null && pmsAttrGroupRelation.getGroupId() != null) {
                pmsAttrVO.setGroupId(pmsAttrGroupRelation.getGroupId());
            }
        }
        return SaResult.ok().setData(pmsAttrVO);
    }

    @Operation(summary = "删除")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "id") @PathVariable Long id) {
        boolean result = pmsAttrService.removeById(id);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "批量删除")
    @DeleteMapping("batchRemove")
    public SaResult remove(@Parameter(description = "id列表") @RequestBody List<Integer> idList) {
        boolean result = pmsAttrService.removeByIds(idList);
        return SaResult.ok().setData(result);
    }
}
